package com.job.java.struts.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.job.java.struts.common.StrutsXmlBean;


public class XMlParse {
	
	Document doc = null; 
	
	/**
	 * @description ��ʼ��XML����
	 * ����֮����Ŀǰ�����ļ���д���ģ����ڿ���������
	 * */
	private  void prepareParse(){
		//����һ��DocumentBuilderFactory��ʵ��
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		//����document��
		DocumentBuilder db;
		try {
			db = dbf.newDocumentBuilder();
			String classPath = this.getClass().getClassLoader().getResource("").getPath()+"struts-config.xml";
			doc = db.parse(classPath);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * @description ��ȡ��Ӧ��bean��Action ��������id��ֵ
	 * @param id
	 * @return actionName
	 * */
	public String getActionNameById(String id){
		prepareParse();
		String actionName = "";
		if(doc != null){
			//��ȡ���е�Bean�ڵ�
			NodeList nodeList = doc.getElementsByTagName("bean");
			for(int i = 0 ;i < nodeList.getLength() ; i++){
				Node node = nodeList.item(i);
				Element element = (Element)node;
				String beanId = element.getAttribute("id");
				if(beanId.equals(id)){
					actionName = element.getAttribute("actionName");
					break;
				}
				//��ȡ�ýڵ��ȵ���Children����Ϣ
				/*�ò��ֿ�������չ���ԵĻ�ȡ
					for(Node childNode = node.getFirstChild();childNode != null;childNode = childNode.getNextSibling()){
						  if (node.getNodeType() == Node.ELEMENT_NODE){
							   //�ӽڵ���Ϣ
							  System.out.println("�ӽڵ����Ϣ...");
							  if(childNode.getFirstChild()!=null){
								  System.out.println(childNode.getNodeName()+":"+childNode.getFirstChild().getNodeValue());
							  }
							 
		                    }
					}
				*/
			}
			
		}else{
			return "";
		}
		return actionName;
	}
	
	/**
	 * @description ��ȡ��Ӧ��bean��Action ��������id��ֵ
	 * @param id
	 * @return actionName
	 * */
	public String getFormNameById(String id){
		prepareParse();
		String formName = "";
		if(doc != null){
			//��ȡ���е�Bean�ڵ�
			NodeList nodeList = doc.getElementsByTagName("bean");
			for(int i = 0 ;i < nodeList.getLength() ; i++){
				Node node = nodeList.item(i);
				Element element = (Element)node;
				String beanId = element.getAttribute("id");
				if(beanId.equals(id)){
					formName = element.getAttribute("formName");
					break;
				}
			}
		}else{
			return "";
		}
		return formName;
	}
	
	
	/**
	 * @description ��ȡ��תҳ���URL
	 * @param id ��Ӧ��BeanID
	 * @param fowwardid 
	 * @return actionName
	 * 
	 * */
	public String getForwardById(String id,String forwardId){
		prepareParse();
		String forward = "";
		if(doc != null){
			//��ȡ���е�Bean�ڵ�
			NodeList nodeList = doc.getElementsByTagName("bean-forward");
			for(int i = 0 ;i < nodeList.getLength() ; i++){
				Node node = nodeList.item(i);
				Element element = (Element)node;
				String forid = element.getAttribute("name");
				if(forid.equals(id)){
					NodeList  forwardNodeList = node.getChildNodes();
					for(int k = 0 ;k < forwardNodeList.getLength() ; k++){
						Node forNode = forwardNodeList.item(k);
						 if (forNode != null  && forNode.getNodeType() == Node.ELEMENT_NODE){
							 Element forElement = (Element)forNode;
							 System.out.println(forElement.getAttribute("id")+"-------"+forElement.getNodeName());
							 if("forward".equals(forElement.getNodeName())){
								 if(forwardId.equals(forElement.getAttribute("id"))){
									 forward = forElement.getAttribute("url");
									 return forward;
								 } 
							 }
						 }
					}
				}
			}
		}else{
			return "";
		}
		return forward;
	}
	
	private static   Document prepareParse(String path){
		//����һ��DocumentBuilderFactory��ʵ��
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		//����document��
		DocumentBuilder db;
		Document doc = null;
		try {
			db = dbf.newDocumentBuilder();
			doc = db.parse(path);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return doc;
	}
	public static Map<String, StrutsXmlBean> initStruts(String xmlPath){
		Map<String, StrutsXmlBean> sxb = new HashMap<String, StrutsXmlBean>();
		Document doc = prepareParse(xmlPath);
		parseDoc(doc,sxb);
		return sxb;
	}

	private static void parseDoc(Document document, Map<String, StrutsXmlBean> sxb) {
		if(document != null){
			//��ȡ���е�Bean�ڵ�
			String id ="";
			NodeList nodeList = document.getElementsByTagName("bean");
			for(int i = 0 ;i < nodeList.getLength() ; i++){
				StrutsXmlBean strutsXmlBean = new StrutsXmlBean();
				Node node = nodeList.item(i);
				Element element = (Element)node;
				id = element.getAttribute("id");
				strutsXmlBean.setNodeId(id);
				strutsXmlBean.setFormName(element.getAttribute("formName"));
				strutsXmlBean.setActionName(element.getAttribute("actionName"));
				strutsXmlBean.setNodeName(element.getNodeName());
				List<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
				if(element.hasChildNodes()){
					NodeList parsetNodeList = element.getElementsByTagName("bean-forward");
					Node bean_forward = null;
					for(int m =0 ; m<parsetNodeList.getLength();m++){
						Element  ele = (Element)parsetNodeList.item(m);
						if(id.equals(ele.getAttribute("name"))){
							bean_forward  = parsetNodeList.item(m);
						}
					}
					
					NodeList  forwardNodeList = bean_forward.getChildNodes();
					for(int k = 0 ;k < forwardNodeList.getLength() ; k++){
						Node forNode = forwardNodeList.item(k);
						 if (forNode != null  && forNode.getNodeType() == Node.ELEMENT_NODE){
							 Element forElement = (Element)forNode;
							 //��forward����Ϣ����map����
							 HashMap<String,String> map = new HashMap<String, String>();
							 map.put("id", forElement.getAttribute("id"));
							 map.put("url", forElement.getAttribute("url"));
							 list.add(map);
						 }
					}
					strutsXmlBean.setChildForward(list);
				}
				sxb.put(id, strutsXmlBean);
				
			}
		}
	}
	
	public static void main(String[] args) {
		XMlParse xmlParse = new XMlParse();
		String xmlPath = xmlParse.getClass().getClassLoader().getResource("").getPath()+"struts-config.xml";
		Map<String, StrutsXmlBean> map = xmlParse.initStruts(xmlPath);
		System.out.println("�������");
		System.out.println("11111");
	}
	
}  